<?php

namespace App\Http\Controllers\Mini\Shop;

use App\Http\Controllers\Controller;
use App\Http\Requests\Mini\Shop\Refund;
use App\Services\OrderService;
use App\Services\RefundCate;
use App\Services\RefundService;
use App\Services\UploadService;
use App\Services\UserService;


/**
 * 退换货控制器
 * Class RefundController
 * @package App\Http\Controllers\Mini\Shop
 */
class RefundController extends Controller
{

    // 订单服务
    public $orderService;
    public $RefundCateService;

    /**
     * RefundController constructor.
     */
    public function __construct()
    {
        $this->orderService = new OrderService();
        $this->RefundCateService = new RefundCate();
    }


    /**
     * 2021/3/11--15:25
     * @param order_id[订单id]
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:refund_before
     * @effect:订单退货之前  退货信息获取
     */
    public function refund_before(Refund $request)
    {
        $id = $request->validated()['order_id'];
        return $this->orderService->getOrderInfoById($id);
    }


    /**
     * 2021/3/11--17:53
     * @param order_id[订单id]
     * @param refund_type [售后类型 0 退款 1 退货]
     * @param refund_remark [退款原因]
     * @param images  [图片]
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:addRefund
     * @effect:申请售后
     */
    public function addRefund(Refund $request, RefundService $refund_service)
    {
        // 验证
        $request->validated();

        // 添加售后
        $rs = $refund_service->add();

        return $rs['status'] ? $this->format($rs['data'], $rs['msg']) : $this->format_error($rs['msg']);
    }

    /**
     * 2021/3/11--21:15
     * @param RefundService $refund_service
     * @param $id
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:showRefund
     * @effect:查看售后详情
     */
    public function showRefund(RefundService $refund_service, Refund $request)
    {
        $id = $request->validated()['order_id'];
        $rs = $refund_service->getRefundByOrderId($id);
        return $rs['status'] ? $this->success($rs['data']) : $this->error($rs['msg']);
    }

    /**
     * 2021/3/11--14:38
     * @param RefundService $refund_service
     * @param $id
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:update
     * @effect:修改售后
     */
    public function update(RefundService $refund_service, Refund $request)
    {
        $id = $request->validated()['order_id'];
        $rs = $refund_service->edit($id);
        return $rs['status'] ? $this->success([], $rs['msg']) : $this->error($rs['msg']);
    }


    /**
     * 2021/3/16--10:42
     * @param order_id[订单ID]
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:clearRefund
     * @effect:取消售后
     */
    public function clearRefund(RefundService $refund_service)
    {
        try {
            return $refund_service->clear_refund();
        } catch (\Exception $e) {
            return $this->format_error('取消失败' . $e->getMessage());
        }
    }


    /**
     * 2021/3/11--20:59
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:refund_upload
     * @effect:售后图片上传
     */
    public function refund_upload(UploadService $upload_service)
    {
        $user_service = new UserService;
        $user_info = $user_service->getUserInfo();
        $rs = $upload_service->refund($user_info['id']);
        if ($rs['status']) {
            return $this->format($rs['data'], $rs['msg']);
        } else {
            return $this->format_error($rs['msg']);
        }
    }

    /**
     * 2021/3/12--17:56
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:getRefundCate
     * @effect:获取售后分类  列表
     */
    public function getRefundCate()
    {
        try {
            $list = $this->RefundCateService->getList(100, false, false);
            return $this->format($list['data']);
        } catch (\Exception $e) {
            return $this->format_error('获取失败' . $e->getMessage());
        }
    }
}

